package game.view.scene
{
	import as3isolib.display.IsoSprite;
	
	import com.greensock.TweenLite;
	import com.greensock.easing.FastEase;
	
	import flash.display.MovieClip;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	import game.controller.GameDispatcher;
	import game.event.EventName;
	import game.event.GameEvent;
	import game.managers.StateManager;
	import game.model.SceneModel;
	
	import org.osmf.events.TimeEvent;

	public class Customer extends Human
	{
		private var _cusSp:IsoSprite;
		private var _tips:IsoSprite;
		private var _tipMC:MovieClip;
		private var _arr:Array=[];
		private var _path:Array;
		private var _timer:Timer;
		private var _timer2:Timer;
		private var _index:int;
		private var _cusTween:TweenLite;
	//	private var _mc:MovieClip;
		private var _model:SceneModel=SceneModel.getInstance();
		/**离开之后的回调函数*/
		private var _leaveFun:Function;
		private var _onLeaveWalking:Boolean=false;
		/**顾客状态
		 * 1-坐
		 * 2-吃
		 * 3-正常行走
		 * */
		public static const CUSTOMER_SIT:int=1;
		public static const CUSTOMER_EAT:int=2;
		public static const CUSTOMER_WALK:int=3;
		public static const CUSTOMER_LEAVE:int=4;

		private var m:int;
		private var _dispatcher:GameDispatcher=GameDispatcher.getInstance();
		public var wait:Boolean=false;

		public function Customer()
		{
			_path=[];
			_timer=new Timer(500);
			
			_nowState=CUSTOMER_WALK;
			_timer2 = new Timer(15000,1);
		}

		override public function move(startPt:Point, endPt:Point):void
		{
			
			if (_nowState == CUSTOMER_LEAVE) 
			{
				onLeaveWalking = true;
				_tipMC.visible=true;
			}
			_path=_model.findPath(startPt, endPt, true);
			
			_timer.start();
			_timer.addEventListener(TimerEvent.TIMER, onEnterFrame);

			_arr=_model.cusDirArr;
			
			m=0;
			_index=0;
		}

		private function onEnterFrame(e:TimerEvent):void
		{
			var targetX:Number=_path[_index].x * 40;
			var targetY:Number=_path[_index].y * 40;

			var dx:Number=targetX - _cusSp.x - 160;
			var dy:Number=targetY - _cusSp.y - 160;
			var dist:Number=Math.sqrt(dx * dx + dy * dy);
			if (dist < 1)
			{
				_index++;
				checkDir();
				if (_index >= _path.length)
				{
					_timer.removeEventListener(TimerEvent.TIMER, onEnterFrame);
					_timer.stop();
					//_timer=null;
					switch (_nowState)
					{
						case 1: /**坐*/
							break;
						case 2: /**吃*/
							break;
						case 3: /**走*/
							changeState(_cusSp, StateManager.customerSitLeft);
							_nowState=CUSTOMER_SIT;
							if (Global.foodDataArr.every(foodDataArrIsNull))
							{
								_dispatcher.dispatchEvent(new GameEvent(EventName.ADD_CUSTOMER));
								/**减少等待时间,不然等待时间过长另一顾客进来坐下
								 * 时会引发厨师连续煮2次菜的bug,会直接导致产生错误
								 * 
								 * 这里要事先将状态设置为leave,不然当第一个走了后厨师会自动煮菜,和scene的removeCustomer
								 * 里的判断函数相矛盾
								 * */
								_nowState=CUSTOMER_LEAVE;
								
								_timer2.addEventListener(TimerEvent.TIMER,onWaiting);
								_timer2.start();
							}else{
								_dispatcher.dispatchEvent(new GameEvent(EventName.COOKER_COOK));
							}
							break;
						case 4:/**离开*/
							/**在离开前将面向右边,在下次进门时则不会出现人物先从后方转向右方
							 * 再开始行走
							 * */
							changeState(_cusSp,StateManager.customerRestRight);
							if (_leaveFun != null)
							{
								_leaveFun();
							}
							_onLeaveWalking = false;
							//_tipMC.visible=false;
							_mc.visible=false;
							
					}
				}

			}
			else
			{
				/**缓动的时间需与timer的时间间隔一致才不会出错或明显的停顿*/
				//	trace("等角坐标" + targetX + "  " + targetY);
				/**每次使用tweenlite都会生成一个新的实例,不控制内存会逐渐加大*/
				if (_nowState == CUSTOMER_LEAVE) 
				{
					_cusTween=TweenLite.to(_cusSp, 0.5, {x: targetX - 160, y: targetY - 160,onUpdate:onLeaving});
				}
				else
					_cusTween=TweenLite.to(_cusSp, 0.5, {x: targetX - 160, y: targetY - 160});
			}
		}
		
		private function onWaiting(e:TimerEvent):void
		{
			_timer2.removeEventListener(TimerEvent.TIMER, onWaiting);		
			_dispatcher.dispatchEvent(new GameEvent(EventName.NO_FOOD));
		}
		
		private function onLeaving():void
		{
			_cusTween=TweenLite.to(_tips,0.5,{x:_cusSp.x - 40,y:_cusSp.y - 55});
		}
		
		override protected function checkDir():void
		{		
			_mc=_cusSp.sprites[0] as MovieClip;
			
			if (_mc.visible == false) 
			{
				_mc.visible = true;
			}
			
			switch (_arr[m])
			{
				case "UP":
					_mc.gotoAndStop(StateManager.customerWalkUp);
					break;
				case "DOWN":
					_mc.gotoAndStop(StateManager.customerWalkDown);
					break;
				case "LEFT":
					_mc.gotoAndStop(StateManager.customerWalkLeft);
					break;
				case "RIGHT":
					_mc.gotoAndStop(StateManager.customerWalkRight);
					break;
			}
			m++;
		}
		
		override public function dispose():void
		{
			super.dispose();
			_mc=null;
			_cusSp=null;
			_path.splice(0);
			_path=null;
			_tips=null;
			_cusTween=null;
			_timer=null;
			_timer2=null;
		}
		
		private function foodDataArrIsNull(item:*, index:int, arr:Array):Boolean
		{
			if (item == null) 
			{
				return true;
			}else{
				return false;
			}
			
		}
		
		public function setTipsPosition():void
		{
			_tips.x = _cusSp.x - 40;
			_tips.y = _cusSp.y - 55;
		}
		
		
		/**
		 * 设置头顶tips为好评
		 * */
		public function setGoodTips():void
		{
			_tipMC.gotoAndStop(StateManager.cusGoodTips);
		}
		
		
		/**
		 * 设置头顶tips为时钟
		 * */
		public function setTimeTips():void
		{
			_tipMC.gotoAndStop(StateManager.cusWaitTips)
		}
		
		
		
		public function get cusSp():IsoSprite
		{
			return _cusSp;
		}

		
		
		public function set cusSp(value:IsoSprite):void
		{
			_cusSp=value;
		}

		
		
		public function get leaveFun():Function
		{
			return _leaveFun;
		}

		
		
		public function set leaveFun(value:Function):void
		{
			_leaveFun=value;
		}	
		
		/**与人物偏差 x=_emsp.x - 40 ,y=_emsp.y-55*/
		public function get tips():IsoSprite
		{
			return _tips;
		}

		/**
		 * @private
		 */
		public function set tips(value:IsoSprite):void
		{
			_tips = value;
			if (tips) 
			{
				_tipMC = _tips.sprites[0] as MovieClip;
				_tipMC.gotoAndStop(StateManager.cusPayTips);
			}
			
		}

		
		
		/**是否正在离开当中*/
		public function get onLeaveWalking():Boolean
		{
			return _onLeaveWalking;
		}

		
		/**
		 * @private
		 */
		public function set onLeaveWalking(value:Boolean):void
		{
			_onLeaveWalking = value;
		}


	}
}